
//1482.制作m束法所需的最少天数
class Solution {
    //检查在t时刻是否满足条件
    bool check(vector<int>& nums,int m,int k,int t)
    {
        int i=0,n=nums.size();
        while(i<n)
        {
            while(i<n&&nums[i]>t) i++;
            int start=i++;
            while(i-start!=k&&i<n&&nums[i]<=t) i++;

            if(i<=n&&i-start==k) m--;
            if(m==0) return true;
        }
        return false;
    }

public:
    int minDays(vector<int>& bloomDay, int m, int k) {
        int n=bloomDay.size();
        if((long long)k*m>n) return -1;  //数量不够直接返回

        int left=0,right=ranges::max(bloomDay);  //取上下边界
        while(left+1<right)
        {
            int mid=left+(right-left)/2;
            if(check(bloomDay,m,k,mid)) right=mid;
            else left=mid;
        }
        return right;
    }
};